package com.jobpass.controller;

import com.jobpass.common.data.ResultData;
import com.jobpass.common.model.Enterprise;
import com.jobpass.common.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

@Slf4j
@Api(tags = "处理Company")
@RequestMapping("company")
@RestController
public class CompanyController {

    @Resource
    EnterpriseService enterpriseServiceImpl;

    private Enterprise enterprise;

    /**
     * 根据学号查询company信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据学号查询company信息", notes = "必须有id")
    @GetMapping("/getCompanyInfo/{id}")
    private ResultData getCompanyInfo(@PathVariable Long id){
        Enterprise enterprise = null;
        try {
            enterprise = enterpriseServiceImpl.findEnterpriseById(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查找失败:"+e);
            ResultData.fail();
        }
        return ResultData.success(enterprise);
    }

    /**
     * 添加company信息
     * @param name
     * @param site
     * @param tel
     * @param mailbox
     * @param license
     * @param introduce
     * @param is_status
     * @return
     */
    @ApiOperation(value = "添加company信息",notes = "必须有company信息")
    @PostMapping("/insertCompanyInfo")
    private ResultData insertCompanyInfo(@RequestParam(required = true) String name,
                                         @RequestParam(required = true) String site,
                                         @RequestParam(required = true) String tel,
                                         @RequestParam(required = true) String mailbox,
                                         @RequestParam(required = true) String license,
                                         @RequestParam(required = true) String introduce,
                                         @RequestParam(required = true) Integer is_status){
        enterprise = new Enterprise(null,null,null,name,site,tel,mailbox,license,introduce,is_status,null,null,null,null);
        try {
            int num = enterpriseServiceImpl.savaEnterprise(enterprise);
            if (num==0){
                return ResultData.fail();
            }
        } catch (Exception e) {
            log.info("添加失败:"+e);
            ResultData.fail();
        }
        return ResultData.success();
    }

    /**
     * 删除company信息
     * @param id
     * @return
     */
    @ApiOperation(value = "删除company信息",notes = "必须有id")
    @DeleteMapping("/delCompanyInfo")
    private ResultData delCompanyInfo(@RequestParam Long id){
        try {
            int num = enterpriseServiceImpl.deleteEnterpriseById(id);
            if (num==0){
                ResultData.fail();
            }
        } catch (Exception e) {
            log.info("删除失败:"+e);
            ResultData.fail();
        }
        return ResultData.success();
    }

    /**
     * 修改company信息
     * @param id
     * @param name
     * @param site
     * @param tel
     * @param mailbox
     * @param license
     * @param introduce
     * @param is_status
     * @return
     */
    @ApiOperation(value = "修改company信息",notes = "必须有company信息")
    @PutMapping("/updateCompanyInfo")
    private ResultData updateCompanyInfo(@RequestParam(required = true) Long id,
                                         @RequestParam(required = true) String name,
                                         @RequestParam(required = true) String site,
                                         @RequestParam(required = true) String tel,
                                         @RequestParam(required = true) String mailbox,
                                         @RequestParam(required = true) String license,
                                         @RequestParam(required = true) String introduce,
                                         @RequestParam(required = true) Integer is_status) {

        enterprise = new Enterprise(id, null, null, name, site, tel, mailbox, license, introduce, is_status, null, null,null,null);
        try {
            int num = enterpriseServiceImpl.updateEnterpriseById(enterprise);
            if (num == 0) {
                return ResultData.fail();
            }
        } catch (Exception e) {
            log.info("添加失败:" + e);
            ResultData.fail();
        }
        return ResultData.success();
    }
}
